package com.weimei.conversionTreasure.weight;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.BottomPopupView;
import com.weigan.loopview.LoopView;
import com.weimei.conversionTreasure.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class BottomTimePop extends BottomPopupView {

    private Context mContext;
    private Click mClick;
    private TextView sure,cancel;

    private List<String> yList = new ArrayList<>();
    private List<String> mList = new ArrayList<>();
    private List<String> dList = new ArrayList<>();

    LoopView yView,mView,dView;

    private String selY,selM,selD;

    private boolean mShowMonth = true;
    private boolean mShowDay = true;


    public interface Click{
        void click(String s);
    }

    public BottomTimePop(@NonNull Context context, Click click,Boolean showMonth,Boolean showDay) {
        super(context);
        this.mContext = context;
        this.mClick = click;
        this.mShowMonth = showMonth;
        this.mShowDay = showDay;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.time_pick;
    }

    @Override
    protected void onCreate() {
        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);

        yView = findViewById(R.id.lv_Y);
        mView = findViewById(R.id.lv_M);
        dView = findViewById(R.id.lv_D);

        if (mShowMonth){
            mView.setVisibility(VISIBLE);
        }else {
            mView.setVisibility(GONE);
        }

        if (mShowDay){
            dView.setVisibility(VISIBLE);
        }else {
            dView.setVisibility(GONE);
        }

        cancel.setOnClickListener(view -> dismiss());
        sure.setOnClickListener(view ->{
            if (mShowMonth){
                if (mShowDay){
                    mClick.click(selY+selM+selD);
                }else {
                    mClick.click(selY+selM);
                }
            }else if (mShowDay){
                mClick.click(selY+selM+selD);
            }else {
                mClick.click(selY);
            }
            dismiss();
        });


        initYLoopView(yView);
        initMLoopView(mView);
        initDLoopView(dView);
    }

    private void initYLoopView(LoopView yView) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        System.out.println("当前年份：" + year);
        if (mShowMonth){
            selY = year+"-";
        }else {
            selY = year+"";
        }
        for (int i = -1; i < 20; i++) {
            yList.add(year+i+"年");
        }
        //滚动监听
        yView.setListener(index -> {
            selY = yList.get(index).replace("年","-");
        });
        //设置原始数据
        yView.setItems(yList);
        //设置初始位置
        yView.setInitPosition(1);
        //设置初始位置-当前
        for (int i = 0; i < yList.size(); i++) {
            int y = Integer.parseInt(yList.get(i).replace("年",""));
            if (year == y){
                yView.setInitPosition(i);
            }
        }
    }

    private void initMLoopView(LoopView mView) {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH)+1;
        System.out.println("当前月份：" + month);
        if (mShowMonth){
            if (mShowDay){
                selM = month+"-";
            }else {
                selM = month+"";
            }
            mList = Arrays.asList(mContext.getResources().getStringArray(R.array.month));
            //滚动监听
            mView.setListener(index -> {
                selM = mList.get(index).replace("月","-");
            });
            //设置原始数据
            mView.setItems(mList);
            //设置初始位置-当前月
            for (int i = 0; i < mList.size(); i++) {
                int mon = Integer.parseInt(mList.get(i).replace("月",""));
                if (month == mon){
                    mView.setInitPosition(i);
                }
            }
        }


    }

    private void initDLoopView(LoopView dView) {

        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("当前天：" + day);
        if (mShowDay){
            selD = day+"";
            dList = Arrays.asList(mContext.getResources().getStringArray(R.array.days));
            //滚动监听
            dView.setListener(index -> {
                selD = dList.get(index).replace("日","");
            });
            //设置原始数据
            dView.setItems(dList);
            //设置初始位置
            //设置初始位置-当前月
            for (int i = 0; i < dList.size(); i++) {
                int d = Integer.parseInt(dList.get(i).replace("日",""));
                if (day == d){
                    dView.setInitPosition(i);
                }
            }
        }

    }


}
